<?php
namespace common\models\im;

use common\models\Common;
use common\models\Status;
use common\services\IM\tengxun\TxIm;
use Yii;
use yii\base\Exception;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class Im extends TxIm
{

    // 未处理的消息通知数量
    public function sendNotReadApplyNum($receiveUserCode = '', $groupNum = 0, $userNum = 0, $circleLikeNum = 0, $circleCollectNum = 0, $attentionNum = 0,
                                        $commentNum = 0,$greetNum = 0,
                                        $interactData = [],$greetData = []
    ) {
        $info = [
            'type' => Status::MESSAGE_TYPE_ADMIN_NOT_READ,
            'group_num' => $groupNum,
            'user_num' => $userNum,
            'circle_like_num' => $circleLikeNum,
            'circle_collect_num' => $circleCollectNum,
            'attention_num' => $attentionNum,
            'comment_num' => $commentNum,
            'greet_num' => $greetNum,
        ];

        if ($interactData) {
            $info['interact_data'] = $interactData;
        }

        if ($greetData) {
            $info['greet_data'] = $greetData;
        }
        return $this->sendUserMessageText($receiveUserCode,$info);
    }

    // 发送单聊问题文本消息
    public function sendSingleText($sendUserCode = '', $receiveUserCode = '', $content = '') {

        return $this->sendUserMessageText($receiveUserCode,$content, $sendUserCode);
    }

    // 更新用户信息
    public function updateUser($txCode = '', $updateData = []) {
        $data = [];
        foreach ($updateData as $key => $value) {
            $tmp = [];
            $tmp['Tag'] = $key;
            $tmp['Value'] = $value;
            $data[] = $tmp;
        }
        return $this->updateImUser(
            $txCode,
            $data
        );
    }

    // 添加好友 TODO
    public function addImFriend() {


    }

    // 更新好友
    public function updateImFriend($userTxCode = '', $friendTxCode = [], $updateData = []) {

        $data = [];

        if (!$userTxCode || !$friendTxCode || !$updateData) {
            return true;
        }

        foreach ($updateData as $key => $value) {
            $tmp = [];
            $tmp['Tag'] = $key;
            $tmp['Value'] = $value;
            $data[] = $tmp;
        }


        return $this->updateFriend(
            $userTxCode,
            $friendTxCode,
            $data
        );
    }


    // 创建群组
    public function addImGroup($groupData = [], $groupHomeUser = [], $groupUser = [], $groupId = 0, $groupFace = '', $groupQrCode = '') {

        $defineData = [
            [
                "Key" => "qr_code",
                "Value" => (string) $groupQrCode,
            ],

            [
                "Key" => "code",
                "Value" => (string) $groupData['code'],
            ],

            # 该字段用于后台群封禁 1-正常 2-封禁
            [
                "Key" => "group_id",
                "Value" => (string) 1,
            ],

            [
                "Key" => "exclusive_red",
                "Value" => "1",
            ],

            [
                "Key" => "is_add_friend",
                "Value" => "1",
            ],

            [
                "Key" => "is_invite_friend",
                "Value" => "1",
            ],

            [
                "Key" => "is_show_qr_code",
                "Value" => "1",
            ],

            [
                "Key" => "is_show_user_n",
                "Value" => "1",
            ],

            [
                "Key" => "is_shut_up",
                "Value" => "2",
            ],

            [
                "Key" => "is_auto_enter",
                "Value" => "2",
            ],

        ];

        if (!$groupUser) {
            return true;
        }

        foreach ($groupUser as $key => $value) {
            $appMemberDefinedData = [
                [
                    'Key' => 'receive_red',
                    'Value' => "1",
                ],
                [
                    'Key' => 'is_shut_up',
                    'Value' => "2",
                ]
            ];
            array_push($appMemberDefinedData,[
                "Key" => "enter_type",
                "Value" => $value['enter_type'] ?? (string) Status::GROUP_USER_JOIN_TYPE_APPLY,
            ]);

            array_push($appMemberDefinedData,[
                "Key" => "invite_code",
                "Value" => $value['invite_code'] ?? "0",
            ]);

            if (isset($value['enter_type'])) {
                unset($groupUser[$key]['enter_type']);
            }

            if (isset($value['invite_code'])) {
                unset($groupUser[$key]['invite_code']);
            }

            $groupUser[$key]['AppMemberDefinedData'] = $appMemberDefinedData;

        }


        return $this->addGroup(
            $groupData['tx_code'],
            $groupHomeUser['tx_code'],
            $groupData['name'],
            $groupFace,
            $groupData['notice'],
            $groupUser,
            $defineData
        );
    }

    // 更新群信息
    public function updateImGroup($groupId = '', $baseGroupData = [],$defineGroupData = []) {
        $data['GroupId'] = $groupId;
        if ($baseGroupData) {
            foreach ($baseGroupData as $key => $value) {
                $data[$key] = $value;
            }
        }

        $appDefinedData = [];
        if ($defineGroupData) {
            foreach ($defineGroupData as $key => $value) {
                $appDefinedData[] = [
                    'Key' => $key,
                    'Value' => $value,
                ];
            }
            $data['AppDefinedData'] = $appDefinedData;
        }

        return $this->updateGroup(
            $data
        );
    }

    // 添加群成员
    public function addImGroupUser($groupId = '', $groupUser = []) {

        if (!$groupUser || !$groupId) {
            return true;
        }

        foreach ($groupUser as $key => $value) {
            $appMemberDefinedData = [];
            $appMemberDefinedData = [
                [
                    'Key' => 'receive_red',
                    'Value' => "1",
                ],
                [
                    'Key' => 'is_shut_up',
                    'Value' => "2",
                ]
            ];
            array_push($appMemberDefinedData,[
                "Key" => "enter_type",
                "Value" => $value['enter_type'] ?? (string) Status::GROUP_USER_JOIN_TYPE_APPLY,
            ]);

            array_push($appMemberDefinedData,[
                "Key" => "invite_code",
                "Value" => $value['invite_code'] ?? "0",
            ]);


            if (isset($value['enter_type'])) {
                unset($groupUser[$key]['enter_type']);
            }

            if (isset($value['invite_code'])) {
                unset($groupUser[$key]['invite_code']);
            }
            $groupUser[$key]['AppMemberDefinedData'] = $appMemberDefinedData;
        }

        return $this->addGroupMember(
            $groupId,
            $groupUser
        );
    }

    // 更新群成员信息
    public function updateImGroupUser($groupId = '', $groupUserTxCode = "", $updateGroupUserData = [],$defineGroupUserData = []) {
        $data['GroupId'] = $groupId;
        $data['Member_Account'] = $groupUserTxCode;
        if ($updateGroupUserData) {
            foreach ($updateGroupUserData as $key => $value) {
                $data[$key] = $value;
            }
        }

        $appMemberDefinedData = [];
        if ($defineGroupUserData) {
            foreach ($defineGroupUserData as $key => $value) {
                $appMemberDefinedData[] = [
                    'Key' => $key,
                    'Value' => $value,
                ];
            }
            $data['AppMemberDefinedData'] = $appMemberDefinedData;
        }

        return $this->updateGroupUser(
            $data
        );
    }

    // 群组消息 - 自定义
    public function sendGroupMessage($groupTxCode = '', $content = [], $fromAccount = '') {
        $msgContent = [
//            'Data' => json_encode($content),
            'Desc' => 'notification',
            'Ext' => '群通知',
            'Sound' => 'dingdong.aiff',
        ];
        switch($content['message_type']) {
            case Status::MESSAGE_TYPE_GROUP_PUBLIC :
                break;

            case Status::MESSAGE_TYPE_GROUP_USER_SEND_RED_PACKET :
                $msgContent = [
//                    'Data' => json_encode($content),
                    'Desc' => 'notification',
                    'Ext' => '群红包',
                    'Sound' => 'dingdong.aiff',
                ];
                break;

            case Status::MESSAGE_TYPE_GROUP_USER_RECEIVE_RED_PACKET :
                $msgContent = [
//                    'Data' => json_encode($content),
                    'Desc' => 'notification',
                    'Ext' => '提示信息',
                    'Sound' => 'dingdong.aiff',
                ];
                break;

            case Status::MESSAGE_TYPE_SEND_USER_BUSINESS_CARD :
                $msgContent = [
                    'Desc' => 'notification',
                    'Ext' => '名片',
                    'Sound' => 'dingdong.aiff',
                ];
                break;


            default :
                $msgContent = [
//                    'Data' => json_encode($content),
                    'Desc' => 'notification',
                    'Ext' => '群通知',
                    'Sound' => 'dingdong.aiff',
                ];
                break;
        }
        unset($content['message_type']);
        $msgContent['Data'] = json_encode($content);
        if ($fromAccount) {
            return $this->sendGroupSingleMessage($groupTxCode, $msgContent, $fromAccount);
        }
        return $this->sendGroupSingleMessage($groupTxCode,$msgContent);
    }

    // 用户消息 - 自定义
    public function sendUserMessage($receiveUserCode = '', $content = [], $fromAccount = '', $isBatch = false) {
        $msgContent = [];
        switch($content['message_type']) {

            case Status::MESSAGE_TYPE_GROUP_USER_SEND_RED_PACKET :
                $msgContent = [
//                    'Data' => json_encode($content),
                    'Desc' => 'notification',
                    'Ext' => '红包',
                    'Sound' => 'dingdong.aiff',
                ];
                break;


            case Status::MESSAGE_TYPE_GROUP_USER_RECEIVE_RED_PACKET :
                $msgContent = [
//                    'Data' => json_encode($content),
                    'Desc' => 'notification',
                    'Ext' => '提示信息',
                    'Sound' => 'dingdong.aiff',
                ];
                break;

            case Status::MESSAGE_TYPE_GROUP_USER_TRANSFER :
                $msgContent = [
//                    'Data' => json_encode($content),
                    'Desc' => 'notification',
                    'Ext' => '转账',
                    'Sound' => 'dingdong.aiff',
                ];
                break;

            case Status::MESSAGE_TYPE_SEND_USER_BUSINESS_CARD :
                $msgContent = [
                    'Desc' => 'notification',
                    'Ext' => '名片',
                    'Sound' => 'dingdong.aiff',
                ];
                break;

            case Status::MESSAGE_TYPE_USER_SYSTEM_NOTICE :
                $msgContent = [
                    'Desc' => 'notification',
                    'Ext' => '系统通知',
                    'Sound' => 'dingdong.aiff',
                ];
                break;

            case Status::MESSAGE_TYPE_USER_MONEY_MESSAGE :
                $msgContent = [
                    'Desc' => 'notification',
                    'Ext' => '财务通知',
                    'Sound' => 'dingdong.aiff',
                ];
                break;

            case Status::MESSAGE_TYPE_USER_SEND_GIFT :
                $msgContent = [
                    'Desc' => 'notification',
                    'Ext' => '礼物',
                    'Sound' => 'dingdong.aiff',
                ];
                break;

        }
        unset($content['message_type']);
        $msgContent['Data'] = json_encode($content);
        if ($fromAccount) {
            return !$isBatch ?
                $this->sendUserSingleMessage($receiveUserCode, $msgContent, $fromAccount) :
                $this->singleUserSendBatchMsg($receiveUserCode, $msgContent, $fromAccount);
        }
        return $this->sendUserSingleMessage($receiveUserCode,$msgContent);
    }

}
